package com.liwy3.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;


public class JwtUtils implements Serializable {
    private static final long serialVersionUID = -3301605591108950415L;

    /**
     * token
     *
     * @param token   token
     * @param signing 签名
     * @return
     * @throws RuntimeException 鉴权信息
     */
    private static Claims getClaimsFromToken(String token, String signing) throws RuntimeException {
        Claims claims;
        try {
            claims = Jwts.parser()
                    .setSigningKey(signing)
                    .parseClaimsJws(token)
                    .getBody();
        } catch (Exception e) {

//            log.error("签名异常：", e);

            throw new RuntimeException();
        }
        return claims;
    }

//    public static Header parse(String token, String signing) {
//        Claims claims = getClaimsFromToken(token, signing);
//        final Date expiration = claims.getExpiration();
//        boolean expire = expiration.before(new Date());
//
//        Header header = new Header();
//        header.setTenantNumber(claims.get(HeaderConstant.TENANT_NUMBER, String.class));
//        header.setChannel(claims.get(HeaderConstant.CHANNEL, Integer.class));
//        header.setNumber(claims.get(HeaderConstant.ACCOUNT_NUMBER, String.class));
//        header.setChannelAccountNumber(claims.get(HeaderConstant.CHANNEL_ACCOUNT_NUMBER, String.class));
//        header.setName(claims.get(HeaderConstant.NAME, String.class));
//        header.setParameters(claims.get(HeaderConstant.PARAMETERS, String.class));
//        header.setExpires(expire);
//        return header;
//    }


    /**
     * @param claims  key 参照 Constant 中的常量设置
     * @param signing 签名
     * @param seconds 有效期（秒）
     * @return
     */
    private static String generateTokenFromMap(Map<String, Object> claims, String signing, Long seconds) {
        Date date = new Date(System.currentTimeMillis() + (seconds * 1000));
        return Jwts.builder()
                .setClaims(claims)
                .setExpiration(date)
                .signWith(SignatureAlgorithm.HS512, signing)
                .compact();
    }


    /**
     * 业务参数
     *
     * @param header  token中存放的数据
     * @param signing 签名
     * @param seconds 超时时间 分钟
     * @return
     */
//    public static String generateToken(Header header, String signing, Long seconds) {
//        Map<String, Object> claims = new HashMap<>();
//        claims.put(HeaderConstant.TENANT_NUMBER, header.getTenantNumber());
//        claims.put(HeaderConstant.CHANNEL, header.getChannel());
//        claims.put(HeaderConstant.ACCOUNT_NUMBER, header.getNumber());
//        claims.put(HeaderConstant.CHANNEL_ACCOUNT_NUMBER, header.getChannelAccountNumber());
//        claims.put(HeaderConstant.NAME, header.getName());
//        claims.put(HeaderConstant.PARAMETERS, header.getParameters());
//        return generateTokenFromMap(claims, signing, seconds);
//    }

//
//
//    /**
//     * 刷新Token
//     *
//     * @param token
//     * @return
//     */
//    public static String refreshToken(Object uid, String token) {
//        if (CACHE.getIfPresent(token) == null) {
//            String refreshedToken;
//            try {
//                final Claims claims = getClaimsFromToken(token);
//                claims.put(CLAIM_KEY_CREATED, new Date());
//                refreshedToken = generateTokenFromMap(claims);
//            } catch (Exception e) {
//                refreshedToken = "";
//            }
//            CACHE.put(uid.toString(), refreshedToken);
//            return refreshedToken;
//        }
//        return null;
//    }


}
